	***********************************************************************
	*Name:			Tables_Figures.do
	*Project:		Network persistance
	*Description:	Runs models of effect of winning
	***********************************************************************
	*Main globals 
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS"	





	********************************************
	*	BANDWIDTH FIGURES
	********************************************

	cd "$dir"
	use "$dir\Data\council_cand_level_persist_rep.dta",clear
	replace rv2=rv2/100
	
	cd "$dir\Figures"
	**Here is where the running variable goes.
	foreach x in rv2 {
	**Here is where the outcome goes.
	*foreach y in $outcomes {
		foreach y in donate_any15 b5 runs_concejo {
		************
		* Linear
		*************
		*mserd approach, linear (get optimal bw)
		rdrobust `y' `x', all vce(cluster muni_code) p(1) level(95) 

		local bw=round(`e(h_r)',0.001)
		local bw_double = `bw'*2
		local bw_half = `bw'/2
		local rho =  `e(h_r)'/`e(b_r)'
		local counter=1
		local b_`counter' = round(`e(tau_cl)',0.001)
		local uci_`counter' = round(`e(ci_r_rb)',0.001)
		local lci_`counter' = round(`e(ci_l_rb)',0.001)
		local bw_`counter' = round(`e(h_r)',0.001)


		*According to Cattaneo, Idrobo and Titiunik, 
		*we should do double of the optimal bandwidht.
		local j = 1
		while `j'>0 {
			local counter = `counter'+1
			
			*CTT approach
			local b = `bw_double'/`rho'
			rdrobust `y' `x', all vce(cluster muni_code) level(95) h(`bw_double') b(`b')
			
			local b_`counter' = round(`e(tau_cl)',0.001)
			local uci_`counter' = round(`e(ci_r_rb)',0.001)
			local lci_`counter' = round(`e(ci_l_rb)',0.001)
			local bw_`counter' = round(`e(h_r)',0.001)
			local bw_double = `bw_double'- (0.005)
			
			if `bw_double'<=`bw_half' {
				local j = 0
				
				* CTT approach
				local counter = `counter'+1
				local b = `bw_double'/`rho'
				rdrobust `y' `x', all vce(cluster muni_code) level(95) h(`bw_double') b(`b')

				local b_`counter' = round(`e(tau_cl)',0.001)
				local uci_`counter' = round(`e(ci_r_rb)',0.001)
				local lci_`counter' = round(`e(ci_l_rb)',0.001)
				local bw_`counter' = round(`e(h_r)',0.001)
					
			
			}
			else {
				local j = 1
			}   
	}


	matrix graph = J(`counter',4,.)
	forvalues i=1/`counter' {
		mat graph[`i',1] = `bw_`i''
		mat graph[`i',2] = `b_`i''
		mat graph[`i',3] = `lci_`i''
		mat graph[`i',4] = `uci_`i''
		
	}

	mata : st_matrix("graph", sort(st_matrix("graph"), 1))
	preserve
	clear 
	svmat graph
	* graphs
	local line_2 = round(`x_line2',0.001)
	*attacks
	graph tw (sc graph2 graph1) (rcap graph4 graph3 graph1, lcolor(dkgreen)), title("") subtitle("") ///
	xtitle("Margin Victory") ytitle("Point Estimate") xli(`bw') xlabel(#8) xscale(range(`bw_half' `bw_double')) legend(off)  graphregion(fcolor(white))  yline(0)
	
	*Save graph
	graph export "Fig_H4_`y'.pdf", as(pdf) replace
	
	restore
	}
	}
	cd "$dir"
	
	
	use "$dir\Data\council_cand_level_persist_rep.dta",clear
		
		replace rv2=rv2/100
	cd "$dir\Figures"
	foreach y in fdonate_any15 fb5{
		************
		* Linear
		*************
		*mserd approach, linear (get optimal bw)
		rdrobust `y' rv2, all vce(cluster muni_code) p(1) level(95) 

		local bw=round(`e(h_r)',0.001)
		local bw_double = `bw'*2
		local bw_half = `bw'/2
		local rho =  `e(h_r)'/`e(b_r)'
		local counter=1
		local b_`counter' = round(`e(tau_cl)',0.001)
		local uci_`counter' = round(`e(ci_r_rb)',0.001)
		local lci_`counter' = round(`e(ci_l_rb)',0.001)
		local bw_`counter' = round(`e(h_r)',0.001)


		*According to Cattaneo, Idrobo and Titiunik, 
		*we should do double of the optimal bandwidht.
		local j = 1
		while `j'>0 {
			local counter = `counter'+1
			
			*CTT approach
			local b = `bw_double'/`rho'
			rdrobust `y' rv2, all vce(cluster muni_code) level(95) h(`bw_double') b(`b')
			
			local b_`counter' = round(`e(tau_cl)',0.001)
			local uci_`counter' = round(`e(ci_r_rb)',0.001)
			local lci_`counter' = round(`e(ci_l_rb)',0.001)
			local bw_`counter' = round(`e(h_r)',0.001)
			local bw_double = `bw_double'- (0.005)
			
			if `bw_double'<=`bw_half' {
				local j = 0
				
				* CTT approach
				local counter = `counter'+1
				local b = `bw_double'/`rho'
				rdrobust `y' rv2, all vce(cluster muni_code) level(95) h(`bw_double') b(`b')

				local b_`counter' = round(`e(tau_cl)',0.001)
				local uci_`counter' = round(`e(ci_r_rb)',0.001)
				local lci_`counter' = round(`e(ci_l_rb)',0.001)
				local bw_`counter' = round(`e(h_r)',0.001)
					
			
			}
			else {
				local j = 1
			}   
	}


	matrix graph = J(`counter',4,.)
	forvalues i=1/`counter' {
		mat graph[`i',1] = `bw_`i''
		mat graph[`i',2] = `b_`i''
		mat graph[`i',3] = `lci_`i''
		mat graph[`i',4] = `uci_`i''
		
	}

	mata : st_matrix("graph", sort(st_matrix("graph"), 1))
	preserve
	clear 
	svmat graph
	* graphs
	local line_2 = round(`margin_victory_line2',0.001)
	*attacks
	graph tw (sc graph2 graph1) (rcap graph4 graph3 graph1, lcolor(dkgreen)), title("") subtitle("") ///
	xtitle("Margin Victory") ytitle("Point Estimate") xli(`bw') xlabel(#8) xscale(range(`bw_half' `bw_double')) legend(off)  graphregion(fcolor(white))  yline(0)
	
	*Save graph
	graph export "Fig_H5_`y'.pdf", as(pdf) replace
	
	restore
	}
	
	
		
	
	
	cd "$dir"
	use "$dir\Data\council_cand_level_persist_rep.dta",clear
	replace rv2=rv2/100	
	cd "$dir\Figures"

	
	**Here is where the running variable goes.
	foreach x in rv2 {
	**Here is where the outcome goes.
	*foreach y in $outcomes {
		foreach y in nfdonate_any15 nfb5 {
		************
		* Linear
		*************
		*mserd approach, linear (get optimal bw)
		rdrobust `y' `x', all vce(cluster muni_code) p(1) level(95) 

		local bw=round(`e(h_r)',0.001)
		local bw_double = `bw'*2
		local bw_half = `bw'/2
		local rho =  `e(h_r)'/`e(b_r)'
		local counter=1
		local b_`counter' = round(`e(tau_cl)',0.001)
		local uci_`counter' = round(`e(ci_r_rb)',0.001)
		local lci_`counter' = round(`e(ci_l_rb)',0.001)
		local bw_`counter' = round(`e(h_r)',0.001)


		*According to Cattaneo, Idrobo and Titiunik, 
		*we should do double of the optimal bandwidht.
		local j = 1
		while `j'>0 {
			local counter = `counter'+1
			
			*CTT approach
			local b = `bw_double'/`rho'
			rdrobust `y' `x', all vce(cluster muni_code) level(95) h(`bw_double') b(`b')
			
			local b_`counter' = round(`e(tau_cl)',0.001)
			local uci_`counter' = round(`e(ci_r_rb)',0.001)
			local lci_`counter' = round(`e(ci_l_rb)',0.001)
			local bw_`counter' = round(`e(h_r)',0.001)
			local bw_double = `bw_double'- (0.005)
			
			if `bw_double'<=`bw_half' {
				local j = 0
				
				* CTT approach
				local counter = `counter'+1
				local b = `bw_double'/`rho'
				rdrobust `y' `x', all vce(cluster muni_code) level(95) h(`bw_double') b(`b')

				local b_`counter' = round(`e(tau_cl)',0.001)
				local uci_`counter' = round(`e(ci_r_rb)',0.001)
				local lci_`counter' = round(`e(ci_l_rb)',0.001)
				local bw_`counter' = round(`e(h_r)',0.001)
					
			
			}
			else {
				local j = 1
			}   
	}


	matrix graph = J(`counter',4,.)
	forvalues i=1/`counter' {
		mat graph[`i',1] = `bw_`i''
		mat graph[`i',2] = `b_`i''
		mat graph[`i',3] = `lci_`i''
		mat graph[`i',4] = `uci_`i''
		
	}

	mata : st_matrix("graph", sort(st_matrix("graph"), 1))
	preserve
	clear 
	svmat graph
	* graphs
	local line_2 = round(`x_line2',0.001)
	*attacks
	graph tw (sc graph2 graph1) (rcap graph4 graph3 graph1, lcolor(dkgreen)), title("") subtitle("") ///
	xtitle("Margin Victory") ytitle("Point Estimate") xli(`bw') xlabel(#8) xscale(range(`bw_half' `bw_double')) legend(off)  graphregion(fcolor(white))  yline(0)
	
	*Save graph
	graph export "Fig_H5_`y'.pdf", as(pdf) replace
	
	restore
	}
	}	
	
	